<template>
    <Card>
        <p slot="title">运营商管理列表</p>
        <div class="search-con search-con-top">
            名称：
            <Input v-model="searchData.name" style="width: 200px"></Input>
            <Button @click="search" type="primary" shape="circle" icon="ios-search"
                style="margin-left: 10px;">搜索</Button>
            <Button @click="reset" type="success" shape="circle" icon="ios-refresh"
                style="margin-left: 10px;">刷新</Button>
            <showRule name="addOperatorBtn">
                <Button @click="showAddDrawer" type="primary" style="float: right;" shape="circle">添加账号</Button>
            </showRule>
        </div>
        <br>

        <Table :columns="columns" :data="list" border>
            <template slot-scope="{ row , index}" slot="areaSlot">
                {{ row.province }}{{ row.city }}
            </template>

            <template slot-scope="{ row , index}" slot="makeSlot">
                <showRule name="editOperatorBtn">
                    <i-button type="primary" size="small" @click="edit(row)">编辑</i-button>
                </showRule>
            </template>

        </Table>
        <br>
        <Page :total="total" show-sizer show-total show-elevator @on-change="changePage"
            @on-page-size-change="changeSize"></Page>
        <!-- 添加管理员抽屉 -->
        <Drawer title="添加运营商" :closable="false" width="40" v-model="showAdd">
            <Form ref="formValidate" :model="formItem" :label-width="100">
                <FormItem label="运营商名称">
                    <Input v-model="formItem.name" placeholder=""></Input>
                </FormItem>
                <FormItem label="联系人">
                    <Input v-model="formItem.contact" placeholder=""></Input>
                </FormItem>
                <FormItem label="电话">
                    <Input v-model="formItem.phone" placeholder=""></Input>
                </FormItem>
                <FormItem label="所属省">
                    <Select v-model="formItem.province_id" filterable @on-change="provinceListChange">
                        <Option v-for="item in provinceList" :value="item.id" :key="item.id">{{ item.name }}</Option>
                    </Select>
                </FormItem>
                <FormItem label="所属市">
                    <Select v-model="formItem.city_id" filterable @on-change="cityListChange">
                        <Option v-for="item in cityList" :value="item.id" :key="item.id">{{ item.name }}</Option>
                    </Select>
                </FormItem>
                <FormItem label="所属区县">
                    <Select v-model="formItem.district_id">
                        <Option v-for="item in areaList" :value="item.id" :key="item.id">{{ item.name }}</Option>
                    </Select>
                </FormItem>
                <FormItem label="详细地址">
                    <Input v-model="formItem.address" placeholder=""></Input>
                </FormItem>
                <FormItem>
                    <Button type="primary" @click="editOperator()">提交</Button>
                </FormItem>
            </Form>
        </Drawer>
    </Card>
</template>

<script>


export default {
    data() {
        return {
            searchData: {
                page: 1,
                limit: 10
            },
            // 列表
            columns: [
                {
                    title: 'ID',
                    key: 'id',
                    minWidth: 50,
                    maxWidth: 100,
                },
                {
                    title: '运营商名称',
                    key: 'name',
                    minWidth: 100,
                },
                {
                    title: '联系人',
                    key: 'contact',
                    minWidth: 100,
                },
                {
                    title: '手机号',
                    key: 'phone',
                    minWidth: 100,
                },
                {
                    title: '所属地区',
                    slot: 'areaSlot',
                },
                {
                    title: '操作',
                    slot: 'makeSlot',
                },
            ],
            // 数据
            list: [],
            total: 0,
            showAdd: false,
            // 管理信息
            formItem: {
                id: 0,
                name: '',
                contact: '',
                phone: '',
                province_id: 0,
                city_id: 0,
                district_id: 0,
                address: ''
            },
            //省列表
            provinceList: [],
            //市列表
            cityList: [],
            //区县列表
            areaList: [],
        }
    },
    methods: {
        search() {
            this.searchData.page = 1;
            this.getList();
        },
        reset() {
            this.searchData = {
                page: 1,
                limit: 10,
            };
            this.getList();
        },
        // 查询管理员信息
        getList() {
            let params = {
                ...this.searchData,
            };
            this.$axios('/adminoperator/getOperatorList', params).then(res => {
                this.list = res.data;
                this.total = res.total;
            });
        },
        changePage(page) {
            this.searchData.page = page;
            this.getList();
        },
        changeSize(size) {
            this.searchData.limit = size;
            this.getList();
        },
        //打开添加管理员抽屉 获取角色列表
        showAddDrawer() {
            this.showAdd = true;
            this.getProvinceList();
        },
        //省列表
        getProvinceList() {
            this.$axios('/adminregion/getProvinceList', {}).then(res => {
                this.provinceList = res;
            });
        },
        //省列表切换
        provinceListChange(value) {
            this.$axios('/adminregion/getCityList', { province_id: value }).then(res => {
                this.cityList = res;
            });
        },
        //市列表切换
        cityListChange(value) {
            this.city_id = value;
            this.$axios('/adminregion/getAreaList', { city_id: value }).then(res => {
                this.areaList = res;
            });
        },
        // 编辑
        edit(info) {
            this.formItem.id = info.id;
            this.formItem.name = info.name;
            this.formItem.contact = info.contact;
            this.formItem.phone = info.phone;
            this.formItem.province_id = info.province_id;
            this.formItem.city_id = info.city_id;
            this.formItem.district_id = info.district_id;
            this.formItem.address = info.address;
            this.showAddDrawer()
        },
        editOperator() {
            this.$axios('/adminoperator/saveOperatorInfo', this.formItem).then(res => {
                this.$Message.success('保存成功');
                this.showAdd = false;
                this.getList();
            });
        }
    },
    created() {
        this.getList();
    }
}
</script>
<style>

</style>
